/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modela2_practica1;

import java.awt.Color;
import java.awt.geom.Ellipse2D;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Marvin
 */
public class graficarDatos {
    final XYSeries series1;
    
    public graficarDatos(String titulo)
    {
        series1 = new XYSeries(titulo);
    }
    
    
    public void resetearSerie()
    {
        series1.clear();
    }
    public ChartPanel getGrafica(String titulo,String ejeY,String ejeX)
    {
        final XYDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset,titulo,ejeY,ejeX);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(100, 100));
        return chartPanel;
    }
    
    public void agregarXY (double X, double Y)
    {
        series1.add(X, Y);
    }
    
    private XYDataset createDataset() {
        XYSeries series = new XYSeries("Caida Libre");
        final XYSeriesCollection dataset = new XYSeriesCollection();
        try {
            dataset.addSeries(series1);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return dataset;
    }

    private JFreeChart createChart(final XYDataset dataset,String titulo,String ejeY,String ejeX) {
        final JFreeChart chart = ChartFactory.createXYLineChart(
            titulo,
            ejeY,
            ejeX,
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);
        chart.setBackgroundPaint(Color.white);
        final XYPlot plot1 = chart.getXYPlot();
        plot1.setBackgroundPaint(Color.lightGray);
        plot1.setDomainGridlinePaint(Color.white);
        plot1.setRangeGridlinePaint(Color.white);

        final XYPlot plot2 = chart.getXYPlot();
        plot2.setBackgroundPaint(Color.lightGray);
        plot2.setDomainGridlinePaint(Color.white);
        plot2.setRangeGridlinePaint(Color.white);
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot1.getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setSeriesShape(0,new Ellipse2D.Double(0,0,5,5));

        return chart;
    }
}
